package com.anolesoft.Planexecution.planweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anolesoft.Planexecution.planweb.dto.PlanWebDto;
import com.anolesoft.Planexecution.planweb.mapper.PlanWebMapper;
import com.anolesoft.Planexecution.planweb.service.PlanWebService;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.MesBzzScjljhDto;
import com.anolesoft.common.core.domain.LoginUser;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlanWebServiceImpl implements PlanWebService {

//    private final String ERPURL = "http://192.168.8.112:8080/services/erpWebService?wsdl";
    private final String ERPURL = "http://172.17.62.246:8082/services/erpWebService?wsdl";

    @Autowired
    private PlanWebMapper mapper;

    /**
     * 去erp获取定额工时-JSONArray
     * @param webDto
     * @return
     * @throws Exception
     */
    @Override
    public String getDegsFromErp(PlanWebDto webDto) throws Exception {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fcode = loginUser.getFacCode();
        String factorycode = mapper.faccode(fcode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Jx",webDto.getMesBzzFcfhjhDo().getJixing());
        jsonObject.put("Th",webDto.getMesBzzFcfhjhDo().getTuhao());
        jsonObject.put("Jfcj",webDto.getMesBzzFcfhjhDo().getJiaofuchejian());
        jsonObject.put("Jscj",webDto.getMesBzzFcfhjhDo().getJieshouchejian());
        jsonObject.put("Jfsx",webDto.getMesBzzFcfhjhDo().getJiaofushunxu());
        jsonObject.put("Bc","46厂暂无版次信息");
        jsonObject.put("Factory",factorycode);
        JSONArray jsonArray= JSONArray.parseArray(JSON.toJSONString(jsonObject));
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(ERPURL);
        Object[] objects = new Object[0];
        objects = client.invoke("未定暂无接口名称", jsonArray.toString());
        Object arrStr = objects[0];
        String jsonObjectStr = arrStr.toString();
        JSONArray degs = JSONArray.parseArray(jsonObjectStr);
        List<PlanWebDto> list = JSONObject.parseArray(degs.toJSONString(), PlanWebDto.class);
        String gs = list.get(0).getDegs();
        return gs;
    }
}
